/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.kontsultak;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import modeloa.InbolukratuModeloa;
import igraf.ItzuliInterfazea;
import igraf.Panela;

public class InbolukratuKontsulta extends Panela{

	
	private static final long serialVersionUID = -4989530970883744040L;
	private InbolukratuModeloa ik;
	private JTable table;
	private JPanel panel;
	private JLabel bilatu;
	private JComboBox aukeratu;
	private JLabel bilaketa;
	private JButton btnBilatu;
	private JTextField bil;
	private JButton atzera;

	public InbolukratuKontsulta(String tituloa,final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);
		JPanel erdikoPanela = this.getErdikoPanela();
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());
	
		ik = new InbolukratuModeloa();
		table = new JTable(ik);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		JPanel xx=new JPanel();
		xx.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(xx);
		
		panel = new JPanel();
		panel.setBackground(SystemColor.activeCaption);
		panel.setLayout(new GridLayout(5,1));
		erdikoPanela.add(panel);
		JPanel bat= new JPanel();
		bat.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		 bat.setLayout(fl_arri);
		bilatu=new JLabel("Aukeratu kontsulta:");
		bilatu.setFont(new Font("Arial", Font.PLAIN, 14));
		aukeratu = new JComboBox();
		aukeratu.addItem("Istripuak");
		aukeratu.addItem("Bidaiarien NAN-ak");
		aukeratu.addItem("Matrikulak");
	
		
		bat.add(bilatu);
		bat.add(aukeratu);
		
		panel.add(bat);
		
		JPanel lau = new JPanel();
		lau.setBackground(SystemColor.activeCaption);
		FlowLayout fl_lau = new FlowLayout();
		fl_lau.setHgap(15);
		fl_lau.setVgap(7);
		lau.setLayout(fl_lau);
		bilaketa=new JLabel("Bilatzekoa:");
		bilaketa.setFont(new Font("Arial", Font.PLAIN, 14));
		bil=new JTextField(20);
		lau.add(bilaketa);
		lau.add(bil);
		btnBilatu = new JButton("Bilatu");
		btnBilatu.setIcon(new ImageIcon(InbolukratuKontsulta.class.getResource(("/Argazkia/lup.png"))));
		lau.add(btnBilatu);
		panel.add(lau);
		JPanel aux = new JPanel();
		aux.setBackground(SystemColor.activeCaption);
		aux.setLayout(new FlowLayout());
	
		JPanel au = new JPanel(new FlowLayout());
		au.setBackground(SystemColor.activeCaption);
		JScrollPane scrollPane = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setPreferredSize(new Dimension(600, 90));
		au.add(scrollPane,BorderLayout.CENTER);
		panel.add(au);
		//behekoa
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setAlignment(FlowLayout.CENTER);
		behekoa.setBackground(SystemColor.activeCaption);
		atzera= new JButton("Atzera");
		atzera.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(atzera);
		add(behekoa, BorderLayout.SOUTH);
		atzera.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
		btnBilatu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!bil.getText().isEmpty()) {// hutsik
					ik.init2(aukeratu.getSelectedIndex() + 1, bil.getText());
					table.updateUI();				
				}else{JOptionPane.showMessageDialog(null,
						"Sartu zerbait zuriunetan", "",
						JOptionPane.INFORMATION_MESSAGE); }
			}
		});}
	
	@Override
	public void eremuakGarbitu() {
    ik.init();		
	}

}
